package com.imedway.ihe.dtz.xds.registry;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLObjectLibrary;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLAdhocQueryRequest30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLExtrinsicObject30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLQueryResponse30;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.AvailabilityStatus;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.query.AdhocQueryRequest;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.query.AdhocQueryResponse;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.ExtrinsicObjectType;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rim.RegistryObjectListType;

import com.imedway.ihe.dtz.xds.cons.SysConsts;
import com.imedway.ihe.dtz.xds.dao.DataDao;
import com.imedway.ihe.dtz.xds.dto.XdsDto;
import com.imedway.ihe.dtz.xds.entity.IheXdsFileInfo;
import com.imedway.ihe.dtz.xds.util.DhccFileUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Iti18Registry_backup {
	
	public AdhocQueryResponse run(AdhocQueryRequest body) {
		EbXMLAdhocQueryRequest30 ebXMLBody= new EbXMLAdhocQueryRequest30(body);
		String patientId = ebXMLBody.getSlots("$XDSDocumentEntryPatientId").toString();		
		log.info(patientId);
		AdhocQueryResponse adhocQueryResponse=new AdhocQueryResponse();
		adhocQueryResponse.setRegistryObjectList(new RegistryObjectListType());
		EbXMLQueryResponse30 ebXMLQueryResponse30= new EbXMLQueryResponse30(new AdhocQueryResponse(),
				new EbXMLObjectLibrary());	
		try {
				
			List<IheXdsFileInfo> iheXdsInfoFileList=find(patientId);
			for(IheXdsFileInfo iheXdsFileInfo:iheXdsInfoFileList) {
									
				EbXMLExtrinsicObject30 ebXMLExtrinsicObject30 = new EbXMLExtrinsicObject30(new ExtrinsicObjectType(),
						new EbXMLObjectLibrary());
				String uniqueId = iheXdsFileInfo.getUniqueId();
				ebXMLExtrinsicObject30.setMimeType(iheXdsFileInfo.getMimeType());
				ebXMLExtrinsicObject30.setId(uniqueId);
				ebXMLExtrinsicObject30.setStatus(AvailabilityStatus.APPROVED);
				ebXMLExtrinsicObject30.addSlot("URI", "http://127.0.0.1:8080/2024/XDS/Repository/"+uniqueId+"."+DhccFileUtils.getFileType(iheXdsFileInfo.getMimeType()));
				ebXMLExtrinsicObject30.addExternalIdentifier(patientId,	SysConsts.XDSDocumentEntrypatientIdidentificationScheme, "XDSDocumentEntry.patientId");
				ebXMLExtrinsicObject30.addExternalIdentifier(uniqueId,SysConsts.XDSDocumentEntryuniqueIdidentificationScheme,"XDSDocumentEntry.uniqueId");
				ebXMLQueryResponse30.addExtrinsicObject(ebXMLExtrinsicObject30);
//				ObjectReference ref = new ObjectReference();
//				ref.setId("urn:uuid:41a5887f-8865-4c09-adf7-e362475b143a");
//				ref.setHome("");
//				
//				ebXMLQueryResponse30.addReference(ref);
			}
			
		} catch (Exception e) {
			log.info(e.getMessage(),e);
		}
		return adhocQueryResponse;
	}
	
	public List<IheXdsFileInfo> find(String patientId) throws SQLException {
		XdsDto xdsDto = new XdsDto();
		List<IheXdsFileInfo> iheXdsInfoFileList = new ArrayList<IheXdsFileInfo>();
		IheXdsFileInfo iheXdsFileInfo =new IheXdsFileInfo();
		iheXdsFileInfo.setPatientId(patientId);
		iheXdsInfoFileList.add(iheXdsFileInfo);
		xdsDto.setIheXdsInfoList(iheXdsInfoFileList);
		DataDao dataDao = new DataDao();
		return dataDao.findFileData(xdsDto);
	}
}
